<?php
/**
 *
 * @author iegss
 *        
 */
class mobileapi_rpc_passport  extends mobileapi_frontpage {
    
    /**
     * 
     * @var base_rpc_service
     */
    var $rpcService;
    
    /**
     */
    function __construct($app)
    {
        $this->app = $app;
        
        $_POST['site_autologin'] = 'on';
        
        //$this->_response->set_header('Cache-Control', 'no-store');
        kernel::single('base_session')->start();
        $this->userObject = kernel::single('b2c_user_object');
        $this->userPassport = kernel::single('b2c_user_passport');
        
        $this->rpcService = kernel::single('base_rpc_service');
    }

     /*
     * 登录验证
     * */
    public function post_login(){
        //_POST过滤
        $userData = array(
            'login_account' => $_POST['uname'],
            'login_password' => $_POST['password']
        );

        

//        if(kernel::single('b2c_service_vcode')->status() && empty($_POST['verifycode'])){
//            //$msg = app::get('b2c')->_('请输入验证码!');
//          $data['needVcode'] = 1;
//          $data['msg'] = '请输入验证码!';
//            $this->rpcService->send_user_error('login_error', $data);
//        }
        $data=app::get('pam')->model('members')->getList('*',array('login_account'=>$userData['login_account']));
        if(!$data){
            //$this->send_vcode_sms($_POST['uname']);
            $data['msg']="该手机号不是会员，需要注册";
            $this->rpcService->send_user_error('need_register', $data);
        }
        $member_id = kernel::single('pam_passport_site_basic')->login($userData,'',$msg,$_POST,true);
        if(!$member_id){
            //设置登陆失败错误次数 一个小时三次错误后需要自动开启验证码
            kernel::single('b2c_service_vcode')->set_error_count();
            $data['needVcode'] = kernel::single('b2c_service_vcode')->status();
            //$this->splash('failed',null,$msg,true,$data);exit;
            $data['msg'] = $msg;
            $this->rpcService->send_user_error('login_error', $data);
        }
        
        

        //记录登录信息
        $b2c_members_model = kernel::single('b2c_mdl_members');
        $member_point_model = kernel::single('b2c_mdl_member_point');
        $member_lv=  kernel::single('b2c_mdl_member_lv');
        
        $member_data = $b2c_members_model->getList( '*', array('member_id'=>$member_id));
        $member_lv_name = $member_lv->getList('name',array('member_lv_id'=>$member_data[0]['member_lv_id']));
     
        if(!$member_data){
            kernel::single('b2c_service_vcode')->set_error_count();
            $data['needVcode'] = kernel::single('b2c_service_vcode')->status();
            //在登录认证表中存在记录，但是在会员信息表中不存在记录
            //$msg = $this->app->_('登录失败：会员数据存在问题,请联系商家或客服');
            //$this->splash('failed',null,$msg,true,$data);exit;
            $data['msg'] = '登录失败：会员数据存在问题,请联系商家或客服';
            $this->rpcService->send_user_error('login_error', $data);
        }
        
        $member_data = $member_data[0];
        $member_data['member_lv_name']=$member_lv_name[0]['name'];
        $member_data['order_num'] = kernel::single('b2c_mdl_orders')->count( array('member_id'=>$member_id) );
        
        /*
        if($this->app->getConf('site.level_switch')==1)
        {
            $member_data['member_lv_id'] = $b2c_members_model->member_lv_chk($member_data['member_lv_id'],$member_data['experience']);
        }
        if($this->app->getConf('site.level_switch')==0)
        {
            $member_data['member_lv_id'] = $member_point_model->member_lv_chk($member_id,$member_data['member_lv_id'],$member_data['point']);
        }
        */

        $b2c_members_model->update($member_data,array('member_id'=>$member_id));
        $this->userObject->set_member_session($member_id);
        $this->bind_member($member_id);
        $this->set_cookie('loginName',$_POST['uname'],time()+31536000);//用于记住密码
        @kernel::single('b2c_mdl_cart_objects')->setCartNum();
        
        $member_data['session'] = $_SESSION;
        
        // 微店
        //$member_data['microshop_info']  = array();
        if ( $member_data['member_type'] == 2 ) {
            $mdl    = app::get('microshop')->model('shop');
            $filter = array (
                'member_id' => $member_data['member_id']
            );
            
            if($mic_info = $mdl->getDetail($filter)) $member_data['microshop_info'] = $mic_info;
        }

        $member_data['avatar']  = $member_data['avatar'] ? kernel::single('base_storager')->image_path($member_data['avatar'],'l') :  kernel::get_resource_host_url()."/headPlaceHolder_big@2x.png";
        $member_data['cover']   = $member_data['cover'] ? kernel::single('base_storager')->image_path($member_data['cover'],'l') : $this->app->res_url.'/images/top-bg.png';
        $model = app::get('b2c')->model('member_coupon');
       
        $filter['disabled'] = 'false';
        $filter['memc_isvalid'] = 'true';
        $filter['member_id'] = $member_id;
        $member_data['count']['coupon'] =  $model->count($filter);
        $model = app::get('b2c')->model('member_goods');
          
        $fav_count = $model->getRow('count(*) total',array('member_id'=>$member_id,'type'=>'fav')); 
        $member_data['count']['fav'] = $fav_count['total'];
        $member_data['count']['point'] = $member_data['point'];
//        $messagecount = app::get('b2c')->model('member_message');
//      
//        $member_data['count']['message'] = $messagecount->count(array('is_read'=>0,'member_id'=>$member_id));
        return $member_data;
    }//end function
    
    
    public function logout(){
        
        $this->unset_member();
        @kernel::single('b2c_mdl_cart_objects')->setCartNum($arr);
        return true;
    }
    
    public function unset_member(){
        $auth = pam_auth::instance(pam_account::get_account_type('b2c'));
        foreach(kernel::servicelist('passport') as $k=>$passport){
            $passport->loginout($auth);
        }
        //$this->app->member_id = 0;
        $this->cookie_path = kernel::base_url().'/';
        $this->set_cookie('MEMBER',null,time()-3600);
        $this->set_cookie('UNAME','',time()-3600);
        $this->set_cookie('MLV','',time()-3600);
        $this->set_cookie('CUR','',time()-3600);
        $this->set_cookie('LANG','',time()-3600);
        $this->set_cookie('S[MEMBER]','',time()-3600);
        foreach(kernel::servicelist('member_logout') as $service){
            $service->logout();
        }
    }
    
    public function send_vcode_email()
    {
        $email = $_POST['uname'];
        $type = trim($_POST['type'])?$_POST['type']:'signup'; //激活activation
    
        if( !$type || !$email ){
            $msg = app::get('b2c')->_('请填写正确的邮箱');
            $this->rpcService->send_user_error('email_error', $msg);
        }
    
        $login_type = $this->userPassport->get_login_account_type($email);
        if($login_type != 'email'){
            $msg = app::get('b2c')->_('请填写正确的邮箱');
            $this->rpcService->send_user_error('email_error', $msg);
        }
    
        if($type == 'reset' && !$this->userPassport->check_signup_account( trim($email),$msg )){
            $this->rpcService->send_user_error('email_error', $msg);
        }
    
        $userVcode = kernel::single('b2c_user_vcode');
        if($email){
            $vcode = $userVcode->set_vcode($email,$type,$msg);
        }
        if($vcode){
            //发送邮箱验证码
            $data['vcode'] = $vcode;
            $data['uname'] = $_POST['uname'];
            $data['msg'] = '邮件已发送';
            if( !$userVcode->send_email($type,(string)$email,$data) ){
                $msg = app::get('b2c')->_('参数错误');
                $this->rpcService->send_user_error('email_p_error', $msg);
            }
            
            return $data;
        }else{
            $this->rpcService->send_user_error('email_vcode_error', $msg);
        }
    }
    
    public function send_vcode_sms($moile){
//        $url = "http://61.145.229.29:9003/MWGate/wmgw.asmx/";
//        $post_arr = array(
//            'userId'=>'J03117',
//            'password'=>'236984',
////            'pszMobis'=>'13533451804',
//            'pszMsg'=>urlencode('员工您好，感谢您对此次测试的配合。'),
//            'iMobiCount'=>'1',
//            'pszSubPort'=>'*',
//            'MsgId'=>'dsadwadwadwad',
//        );
//        $msg = $post_arr['pszMsg'];//"【樱桃社】 ".$msg; // 调试用的
//        $re = file_get_contents($url."MongateSendSubmit?userId={$post_arr['userId']}&password={$post_arr['password']}&pszMobis={$mobile}&pszMsg={$msg}&iMobiCount={$post_arr['iMobiCount']}&pszSubPort={$post_arr['pszSubPort']}&MsgId={$post_arr['MsgId']}");
//        return $url."MongateSendSubmit?userId={$post_arr['userId']}&password={$post_arr['password']}&pszMobis={$mobile}&pszMsg={$msg}&iMobiCount={$post_arr['iMobiCount']}&pszSubPort={$post_arr['pszSubPort']}&MsgId={$post_arr['MsgId']}";

        $mobile = $mobile ? $mobile : trim($_POST['uname']);
        $type = trim($_POST['type'])?$_POST['type']:'signup'; //激活activation signup forgot
        if($type === "signup"){
            $pam_members_model = app::get('pam')->model('members');
            $data = $pam_members_model->getList('member_id',array('login_account'=>$mobile));
            if($data){
            $msg = app::get('b2c')->_('该手机号已注册，请返回登录或重置密码');
                $this->rpcService->send_user_error('send_vcode_sms_error', $msg);
            }
        }
        if( !$type || !$mobile ){
            $msg = app::get('b2c')->_('用户名不能为空');
            //$this->splash('failed',null,$msg,true);
            $this->rpcService->send_user_error('send_vcode_sms_error', $msg);
        }
    
        $login_type = $this->userPassport->get_login_account_type($mobile);
        if($login_type != 'mobile'){
            $msg = app::get('b2c')->_('请填写正确的手机号码');
            //$this->splash('failed',null,$msg,true);
            $this->rpcService->send_user_error('send_vcode_sms_error', $msg);
        }
    
        if($type == 'reset' && !$this->userPassport->check_signup_account( trim($mobile),$msg )){
            //$this->splash('failed',null,$msg,true);
           
            $this->rpcService->send_user_error('send_vcode_sms_error', $msg);
        }
    
        $userVcode = kernel::single('b2c_user_vcode');
        if($mobile){
            $vcode = $userVcode->set_vcode($mobile,$type,$msg);
        }
        if($vcode){
            //发送验证码 发送短信
            
            $data['vcode'] = $vcode;
            $data['uname'] = $_POST['uname'];
            $data['msg'] = '短信已发送';
            if( !$userVcode->send_sms($type,(string)$mobile,$data) ){
                $msg = app::get('b2c')->_('发送失败');
                //$this->splash('failed',null,$msg,true);
                $this->rpcService->send_user_error('send_vcode_sms_error', $msg);
            }
            unset($data['vcode']);
            return $data;
            
        }else{
            //$this->splash('failed',null,$msg,true);
            $this->rpcService->send_user_error('send_vcode_sms_error', $msg);
        }
    }
    
    
    /**
     * create
     * 创建会员
     * 采用事务处理,function save_attr 返回false 立即回滚
     * @access public
     * @return void
     */
    public function member_create(){
        $in_post['response_json'] = $_POST['response_json'] = 'true';
        $in_post['license'] = $_POST['license'] = 'on';
        
        $in_post['pam_account']['login_name'] = trim($_POST['uname']);
        $in_post['pam_account']['psw_confirm'] = $_POST['password'];
        $in_post['pam_account']['login_password'] = $_POST['password'];
        //return $_POST;
        $in_post['vcode'] = trim($_POST['vcode']);
        
        
        if($in_post['response_json'] == 'true'){
            $ajax_request = true;
        }else{
            $ajax_request = false;
        }
        if( !$this->userPassport->check_signup($in_post,$msg) ){
            //$this->splash('failed',null,$msg,$ajax_request);
            $this->rpcService->send_user_error('member_create_error', $msg);
        }
    
        $saveData = $this->userPassport->pre_signup_process($in_post);
        
        //print_r($saveData);exit;
    
        if( $member_id = $this->userPassport->save_members($saveData,$msg) ){
            $this->userObject->set_member_session($member_id);
            $this->bind_member($member_id);
            foreach(kernel::servicelist('b2c_save_post_om') as $object) {
                $object->set_arr($member_id, 'member');
                $refer_url = $object->get_arr($member_id, 'member');
            }
            /*注册完成后做某些操作! begin*/
            foreach(kernel::servicelist('b2c_register_after') as $object) {
                $object->registerActive($member_id);
            }
            //增加会员同步 2012-5-15
            if( $member_rpc_object = kernel::service("b2c_member_rpc_sync") ) {
                $member_rpc_object->createActive($member_id);
            }
            /*end*/
            $data['member_id'] = $member_id;
            $data['uname'] = $saveData['pam_account']['login_name'];
            //$data['passwd'] = $in_post['pam_account']['psw_confirm'];返回密码明文
            $data['email'] = $in_post['contact']['email'];
            $data['refer_url'] = $refer_url ? $refer_url : '';
            $data['is_frontend'] = true;
            $obj_account=app::get('b2c')->model('member_account');
            $obj_account->fireEvent('register',$data,$member_id);
            /*
            if(!strpos($_SESSION['pc_next_page'],'cart')){
                $url = $this->gen_url(array('app'=>'b2c','ctl'=>'site_passport','act'=>'sign_tips'));
            }else{
                $url = $_SESSION['pc_next_page'];
            }
            */
            //$this->splash('success',$url,app::get('b2c')->_('注册成功'),$ajax_request);
            app::get('b2c')->model('members')->update(array('source_app'=>$_POST['source_app'],'source'=>'api'),array('member_id'=>$member_id));
                
            return $data;
        }
        
        
        //$this->splash('failed',$back_url,app::get('b2c')->_('注册失败'),$ajax_request);
        $this->rpcService->send_user_error('member_create_error', $msg);
        
    }
    
    
    public function sendPSW(){
        $username = $_POST['username'];
        $member_id = $this->userObject->get_member_id_by_username($username);
    
        if(!$member_id){
            $msg = app::get('b2c')->_('该账号不存在，请检查');
            $this->rpcService->send_user_error('member_error', $msg);
        }
    
        $pamMemberData = app::get('pam')->model('members')->getList('*',array('member_id'=>$member_id));
        foreach($pamMemberData as $row){
            if($row['login_type'] == 'mobile' && $row['disabled'] == 'false'){
                $data['mobile'] = $row['login_account'];
                $verify['mobile'] = true;
            }
    
            if($row['login_type'] == 'email' && $row['disabled'] == 'false'){
                $data['email'] = $row['login_account'];
                $verify['email'] = true;
            }
        }
    
        if($verify['mobile'] || $verify['email']){
            $this->pagedata['send_status'] = 'true';
        }else{
            $this->rpcService->send_user_error('member_verify_error', '由于您并未验证手机或者邮箱，无法自助找回密码，请联系网站客服！');
        }
        
        $data['send_type'] = $row['login_type'];
        
        $this->pagedata['data'] = $data;
        
        return $data;
    }
    
    public function resetpwd_code(){
        $send_type = $_POST['send_type']; //email  mobile   vcode   
        $userVcode = kernel::single('b2c_user_vcode');
        if( !$vcodeData = $userVcode->verify($_POST['vcode'],$_POST['username'],'forgot')){
            $msg = app::get('b2c')->_('验证码错误');
            $this->rpcService->send_user_error('verify_error', $msg);
        }
        $data['key'] = $userVcode->get_vcode_key($_POST[$send_type],'forgot');
        $data['key'] = md5($vcodeData['vcode'].$data['key']);
        $data['account'] = $_POST['username'];
        return $data;
    }
    
    public function resetpassword(){
        /*  
        $userVcode = kernel::single('b2c_user_vcode');
        $vcodeData = $userVcode->get_vcode($_POST['account'],'forgot');
        $key = $userVcode->get_vcode_key($_POST['account'],'forgot');
        //print_r($vcodeData); echo md5($vcodeData['vcode'].$key);exit;
        if($_POST['account'] !=$vcodeData['account']  || $_POST['key'] != md5($vcodeData['vcode'].$key) ){
            $msg = app::get('b2c')->_('页面已过期,请重新找回密码');
            $this->rpcService->send_user_error('pages_error', $msg);
        }
        */
    
        if( !$this->userPassport->check_passport($_POST['login_password'],$_POST['psw_confirm'],$msg) ){
            $this->rpcService->send_user_error('pwconfirm_error', $msg);
        }
    
        $member_id = $this->userObject->get_member_id_by_username($_POST['account']);
        if( !$this->userPassport->reset_passport($member_id,$_POST['login_password']) ){
            $msg = app::get('b2c')->_('密码重置失败,请重试');
            $this->rpcService->send_user_error('resetpw_error', $msg);
        }
        
        return array('status' =>'ok','msg'=>'新密码已经设置成功，请使用新密码重新登录' );
    }
    
    
    
    
    
    /**
     * 第三方登录
     * @param unknown $data
     */
    public function trust_login($post){
        if( !$this->userObject->is_login() ){

            /*
            openid: "1707357339",
            provider_code: "qzone",
            provider_openid: "ADB189CA2182398213E6FC0905BB0333",
            nickname: "是非亦航",
            realname: "是非亦航",
            avatar: "http://qzapp.qlogo.cn/qzapp/100278270/ADB189CA2182398213E6FC0905BB0333/100",
            email: "",
            gender: "1",
            address: "中国 广东 广州",
            province: "广东",
            city: "广州"
            */

            $data['provider_code'] = $post['provider_code'];
            $data['openid'] = $post['openid'];
            $data['nickname'] = $post['nickname']?$post['nickname']:'';
            $data['realname'] = $post['realname']?$post['realname']:'';
            $data['avatar'] = $post['headimgurl']?$post['headimgurl']:'';
            $data['reg_ip'] = base_request::get_remote_addr();
            $data['regtime'] = time();
            switch($post['sex']){
                case 0:
                    $data['sex'] = 1;
                    break;
                case 1:
                    $data['sex'] = 2;
                    break;
                case 2:
                    $data['sex'] = 0;
                    break;
                default:
                    $data['sex'] = 0;
                    break;
            }
            $data['addr'] = $post['country'].$post['province'].$post['city'];
            $data['email'] = $post['email']?$post['email']:'';
            $data['gender'] = $post['es']?$post['gender']:"2";

            $result['data'] = $data;
            $login_name = $this->trust_save_login_data($result,$msg);
            if(!$login_name)$this->rpcService->send_user_error('login_error', $msg);

            $row = app::get('pam')->model('auth')->getList('account_id,module_uid',array('module_uid'=>$login_name));
            $member_id = $row[0]['account_id'];

            if(!$member_id)$this->rpcService->send_user_error('login_error', '登录ID错误');
        }else{
            $this->member = $this->get_current_member();
            $member_id = $this->member->member_id;
        }//1

        //记录登录信息
        $b2c_members_model = kernel::single('b2c_mdl_members');
        $member_point_model = kernel::single('b2c_mdl_member_point');

        $member_data = $b2c_members_model->getList( '*', array('member_id'=>$member_id) );

        if(!$member_data){
            kernel::single('b2c_service_vcode')->set_error_count();
            $data['needVcode'] = kernel::single('b2c_service_vcode')->status();
            //在登录认证表中存在记录，但是在会员信息表中不存在记录
            //$msg = $this->app->_('登录失败：会员数据存在问题,请联系商家或客服');
            //$this->splash('failed',null,$msg,true,$data);exit;
            $data['msg'] = '登录失败：会员数据存在问题,请联系商家或客服';
            $this->rpcService->send_user_error('login_error', $data);
        }

        $member_data = $member_data[0];
        $member_data['order_num'] = kernel::single('b2c_mdl_orders')->count( array('member_id'=>$member_id) );

        /*
         if($this->app->getConf('site.level_switch')==1)
         {
        $member_data['member_lv_id'] = $b2c_members_model->member_lv_chk($member_data['member_lv_id'],$member_data['experience']);
        }
        if($this->app->getConf('site.level_switch')==0)
        {
        $member_data['member_lv_id'] = $member_point_model->member_lv_chk($member_id,$member_data['member_lv_id'],$member_data['point']);
        }
        */

        $b2c_members_model->update($member_data,array('member_id'=>$member_id));
        $this->userObject->set_member_session($member_id);
        $this->bind_member($member_id);

        $this->set_cookie('loginName',$_POST['uname'],time()+31536000);//用于记住密码

        @kernel::single('b2c_mdl_cart_objects')->setCartNum();

        $member_data['session'] = $_SESSION;//3

        // 微店
        //$member_data['microshop_info']  = array();
        if ( $member_data['member_type'] == 2 ) {
            $mdl    = app::get('microshop')->model('shop');
            $filter = array (
                'member_id' => $member_data['member_id']
            );
            
            if($mic_info = $mdl->getDetail($filter)) $member_data['microshop_info'] = $mic_info;
        }

        $member_data['avatar']  = $member_data['avatar'] ? kernel::single('base_storager')->image_path($member_data['avatar'],'l') :  kernel::get_resource_host_url()."/headPlaceHolder_big@2x.png";
        $member_data['cover']   = $member_data['cover'] ? kernel::single('base_storager')->image_path($member_data['cover'],'l') : $this->app->res_url.'/images/top-bg.png';
        app::get('b2c')->model('members')->update(array('source'=>'api'),array('member_id'=>$member_id));
        return $member_data;
         
    }
    
    function trust_save_login_data($result,&$msg){
        $saveData['b2c_members'] = $this->pre_b2c_members_data($result);
        $saveData['pam_account'] = $this->pre_pam_members_data($result);

        $row = app::get('pam')->model('auth')->getList('auth_id,module_uid',array('module_uid'=>$saveData['pam_account']['login_account']));
        $account = app::get('pam')->model('members')->getList('member_id',array('login_account'=>$saveData['pam_account']['login_account']));
        if($row && $account){//已有信息不用再次保存
        $saveData['b2c_members']['member_id'] = $account[0]['member_id'];
        kernel::single('b2c_mdl_members')->save($saveData['b2c_members']);
            return $saveData['pam_account']['login_account'];
        }

        $member_model = kernel::single('b2c_mdl_members');
        $db = kernel::database();
        $db->beginTransaction();
        //保存到b2c members
        if( $member_model->insert($saveData['b2c_members']) ){
            $member_id = $saveData['b2c_members']['member_id'];
            if(!(kernel::single('b2c_user_passport')->save_attr($member_id,$saveData['b2c_members'],$msg))){
                $db->rollBack();
                return false;
            }
    
            $saveData['pam_account']['member_id'] = $member_id;
            if(!app::get('pam')->model('members')->save($saveData['pam_account'])){
                $db->rollBack();
                return false;
            }
    
            $authData = array(
                    'account_id'=>$member_id,
                    'module_uid'=>$saveData['pam_account']['login_account'],
                    'module'=>'openid_passport_trust',
            );
            if($row[0]['auth_id']){
                $authData['auth_id'] = $row[0]['auth_id'];
            }
            if( !app::get('pam')->model('auth')->save($authData) ){
                $db->rollBack();
                return false;
            }
            
            
            /*
            $openidData = $this->pre_openid_data($member_id,$result);
            if( !app::get('openid')->model('openid')->save($openidData) ){
                $db->rollBack();
                return false;
            }
            */
            
        }else{
            return false;
        }
        $db->commit();
        return $saveData['pam_account']['login_account'];
    }
    
    public function pre_b2c_members_data($result){
        $lv_model = app::get('b2c')->model('member_lv');
        $member_lv_id = $lv_model->get_default_lv();
        $data['member_lv_id'] = $member_lv_id;
        $arrDefCurrency = app::get('ectools')->model('currency')->getDefault();
        $data['currency'] = $arrDefCurrency['cur_code'];
        $data['email'] = $result['data']['email'];
        $data['name'] = empty($result['data']['nickname']) ? $result['data']['realname'] : $result['data']['nickname'];
        $data['addr'] = $result['data']['addr'];
        $data['sex'] = $result['data']['sex'];
        $data['trust_name'] = empty($result['data']['nickname'])?$result['data']['realname']:$result['data']['nickname'];
        $data['reg_ip'] = $result['data']['reg_ip'];
        $data['regtime'] = $result['data']['regtime'];
        $image = $result['data']['avatar'] ? $this->imageStore($result['data']['avatar'],3) : "";
//        $image = $result['data']['avatar'] ? $result['data']['avatar'] : "";
        $data['avatar'] = $image ? $image['image_id'] : "";
        return $data;
    }
    
    public function pre_pam_members_data($result){
        $data = $result['data'];
        if(empty($data['nickname'])){
            $login_name = $data['provider_code'].'_'.$data['realname'].'_'.$data['openid'];
        }else{
            $login_name = $data['provider_code'].'_'.$data['nickname'].'_'.$data['openid'];
        }
    
        $return = array(
                'login_type' => 'local',
                'login_account' => $login_name,
                'login_password' => md5(time().$login_name),
                'password_account' => $login_name, //登录密码加密账号
                'disabled' =>  'false',
                'createtime' => time()
        );
        return $return;
    }
    
    public function pre_openid_data($member_id,$result){
        $save= array(
                'member_id' => $member_id,
                'openid' => $result['data']['openid'],
                'provider_code' => (string)$result['data']['provider_code'],
                'provider_openid' => (string)$result['data']['provider_openid'],
                'avatar' => $result['data']['avatar'],
                'email' => $result['data']['email'],
                'address' => $result['data']['address'],
                'gender' => $result['data']['gender'],
                'nickname' => $result['data']['nickname'],
                'realname' => $result['data']['realname'],
        );
        return $save;
    }
    //图片上传公共类
    protected function imageUpload($fileurl,$max_size = 3, $size = '') {
        if (empty($size)) {
            $size == array('width' => '400', 'height' => '300');
        }
        //return $fileurl;
//        $filename = md5(time().$this->rand_string());
//        $file = file_get_contents($fileurl);
//        file_put_contents("./public/files/".$filename.".jpg", $img);
//        return time();
        $mdl_img = app::get('image')->model('image');

//        $image_name = $filename.".jpg";
        $image_id = $mdl_img->store($fileurl, null, null, $image_name);

        $mdl_img->rebuild($image_id, array('l', 'm', 's'));
        $image_src = base_storager::image_path($image_id, 'l');
        //return 'baid.com';
        if (empty($image_id)) {
            $this->rpcService->send_user_error('4013', '文件上传失败，请重新上传');
            exit();
        }
        return array(
            'res' => 'success',
            'image_id' => $image_id,
            'image_url' => $image_src
        );
    }

       protected function imageStore($fileurl, $max_size = 3){
           $params['image_id'] = app::get('image')->model('image')->gen_id();
           $params['image_name'] = null;
           $params['storage'] = 'network';
           $params['url'] = $fileurl;
           $params['ident'] = $fileurl;
           $params['width'] = 400;
           $params['height'] = 300;
           app::get('image')->model('image')->save($params);
           return array(
       'res' => 'success',
       'image_id' => $params['image_id'],
       'image_url' => $params['url']
           );
       }

        function rand_string( $length = 6 ) {  
            // 密码字符集，可任意添加你需要的字符  
            $chars = 'abcdefghijklmnopqrstuvwxyz';  
            $password = '';  
            for ( $i = 0; $i < $length; $i++ )  
            {  
            // 这里提供两种字符获取方式  
            // 第一种是使用 substr 截取$chars中的任意一位字符；  
            // 第二种是取字符数组 $chars 的任意元素  
             $password .= substr($chars,mt_rand(0,strlen($chars)-1),1);  
                //$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];  
            }  
            return $password;  
        } 
    
}

?>
